<template>
	<view class="shoucang">
		<headertop str1="oridei" title="我的收藏"></headertop>
		<view class="shoucangback">

		</view>
		<view class="centerbox">
			<view class="linetop">
				<view class="title">
					全部收藏（{{total}}）
				</view>
				<view class="glsc">
					<span @click="qiehuan">{{flag?'取消收藏':'管理收藏'}} </span> <span v-if="flag" @click="flag=!flag" style="margin-left: 30rpx;
    color: red;">关闭</span>
				</view>
			</view>
			<view class="allspbox">
				<view class="msgicon" v-if="!list.length">
					<image src="https://shop.hacuu.com/static/Frame@2x(21).png" mode="widthFix"></image>
					<view class="msgtext">
						暂无内容
					</view>
				</view>
				<view class="itembox" v-for="item in  list" :key="item.id" @click="gopage(item.goods.id)">

					<image :src="item['goods']['image']" class="fmimg" mode=""></image>
					<view class="texts">
						<view class="t1">
							{{item.goods.title}}
						</view>
						<view class="t2">
							{{item.goods.subtitle}}
						</view>
						<view class="jiageline">
							<view class="jige">
								￥{{jigaefun(item.goods.price[0])}}.<span
									style="font-size: 20rpx;">{{jigaefun(item.goods.price[0],'后')}}</span>
							</view>
							<image
								:src="item.bool?'https://shop.hacuu.com/static/Group 11469@2x (1).png':'https://shop.hacuu.com/static/Ellipse 2226@2x.png'"
								@click.stop="item.bool=!item.bool" class="gwcicon" v-if="flag" mode=""></image>
							<image src="http://shop.hacuu.com/static/Frame@2x(3).png" v-else class="gwcicon" mode="">
							</image>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import headertop from '@/components/header.vue'
	import {
		onLoad,
		onPageScroll
	} from '@dcloudio/uni-app'
	import {
		ref,
		reactive
	} from 'vue'
	import {
		sclist
	} from '@/api/my.js'

	import {
		favorite
	} from '@/api/home.js'
	const query = reactive({
		type: 'favorite',
		list_rows: 10,
		page: 1
	})
	const total = ref(0)
	const lastpage = ref(0)
	const flag = ref(false)
	const list = ref([])
	onLoad(() => {
		init()
	})

	function jigaefun(str1, strr) {
		let number = str1
		let str = number.toString(); // 转换为字符串 "100.00"
		let parts = str.split('.'); // 按小数点分割
		let integerPart = parts[0]; // "100"
		let decimalPart = parts[1] || "00"; // "00"（如果没有小数部分，默认 "00"）
		if (!strr) {
			return integerPart
		} else {
			return decimalPart
		}

	}
	async function qiehuan() {
		if (!flag.value) {
			flag.value = !flag.value
		} else {
			var arr = list.value.filter(item => item.bool)
			if (!arr.length) {
				uni.showToast({
					title: '请选择商品',
					icon: 'none'
				})
				return
			}
			var ids = arr.map(item => item.goods_id)
			const res = await favorite({
				goods_id: ids.join()
			})
			if (res.code == 1) {
				setTimeout(() => {
					uni.showToast({
						title: '取消收藏成功'
					})
				}, 500)
				query.page = 1
				list.value = []
				flag.value = false
				init()
			}

		}
	}
	async function init() {
		const res = await sclist(query)
		if (res.code == 1) {
			list.value.push(...res.data.data)
			for (var i in list.value) {
				list.value[i]['bool'] = false
			}
			lastpage.value = res.data.last_page
			total.value = res.data.total
		}
	}

	function gopage(id) {
		uni.navigateTo({
			url: '/pages/home/shangpdetail?id=' + id
		})
	}
	onPageScroll((e) => {

		if (lastpage.value > query.page) {
			query.page++
			init()

		}
	})
</script>

<style lang="scss">
	.msgicon {
		width: 608rpx;
		margin: 100rpx auto;
		overflow: hidden;

		.msgtext {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #86909C;
			text-align: center;
			width: 100%;
			margin-top: -30rpx;
		}

		image {
			width: 608rpx;
		}
	}

	.shoucang {
		width: 100%;
		position: relative;
		overflow: hidden;

		.shoucangback {
			width: 750rpx;
			height: 314rpx;
			background: linear-gradient(180deg, #FCE6CD 0%, rgba(252, 230, 205, 0) 100%);
			position: absolute;
			z-index: -1;
		}

		.centerbox {
			width: 686rpx;
			margin: auto;
			margin-top: 184rpx;
			overflow: hidden;

			.allspbox {
				margin-top: 28rpx;
				overflow: hidden;
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				align-items: center;
				width: 100%;

				.itembox {
					width: 334rpx;
					height: 492rpx;
					background: #F7F8FA;
					border-radius: 20rpx 20rpx 20rpx 20rpx;
					overflow: hidden;
					display: flex;
					flex-direction: column;
					align-items: center;
					margin-bottom: 24rpx;
					position: relative;

					.checkiocn {
						position: absolute;
						width: 32rpx;
						height: 32rpx;
						top: 0%;
						right: 0%;
					}

					.fmimg {
						width: 318rpx;
						height: 318rpx;
						border-radius: 16rpx 16rpx 0rpx 0rpx;
					}

					.texts {
						width: 318rpx;
						margin: auto;
						overflow: hidden;
						margin-top: 18rpx;

						.jiageline {
							width: 100%;
							display: flex;
							justify-content: space-between;
							align-items: center;
							margin-top: 24rpx;

							.jige {
								font-family: PingFang SC, PingFang SC;
								font-weight: bold;
								font-size: 32rpx;
								color: #F53F3F;
							}

							.gwcicon {
								width: 36rpx;
								height: 36rpx;
								margin-right: 24rpx;
							}
						}

						.t1 {
							font-family: PingFang SC, PingFang SC;
							font-weight: 500;
							font-size: 24rpx;
							color: #1D2129;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 1;
							/* 限制行数为2 */
							-webkit-box-orient: vertical;
						}

						.t2 {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 22rpx;
							color: #4E5969;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 1;
							/* 限制行数为2 */
							-webkit-box-orient: vertical;
							margin-top: 14rpx;
						}
					}
				}
			}

			.linetop {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.title {
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #4E5969;
				}

				.glsc {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #1D2129;
				}
			}
		}
	}
</style>